#include "joueur.h"



  void initializePlayer(void)
   {

       
       player.personnage[0] = IMG_Load("perso/habib0.png");
       player.personnage[1] = IMG_Load("perso/habib1.png");
       player.personnage[2] = IMG_Load("perso/habib2.png");
       player.personnage[3] = IMG_Load("perso/habib3.png");
       player.personnage[4] = IMG_Load("perso/habib4.png");

     

       player.direction = RIGHT;
       player.etat = IDLE;
       
       player.NumAnim = 0;

       player.x = 0;
       player.y = 0;
       player.dirX=0;
   
    
       player.w = PLAYER_WIDTH;
       player.h = PLAYER_HEIGTH;

   
       player.timerMort = 0;
       player.onGround = 0;
       jeu.nombreMonstres = 0;
       jeu.nombreButin = 0;
       jeu.gameOver=0;

   }


     void drawplayer()
{
    
    SDL_Rect dest;

    dest.x = player.x - map.startX;
    dest.y = player.y - map.startY;
    dest.w = PLAYER_WIDTH;
    dest.h = PLAYER_HEIGTH;

   

    SDL_BlitSurface(player.personnage[player.NumAnim], NULL, jeu.ecran, &dest);
    SDL_Flip(jeu.ecran);


}


 void updatePlayer(int maxW, int maxH)
{


  if (player.timerMort == 0)
  {

    player.dirX = 0;

    player.dirY += GRAVITY_SPEED;

    if (player.dirY >= MAX_FALL_SPEED)
    {
        player.dirY = MAX_FALL_SPEED;
        Mix_Pause(20);
    }

    
     if (input.left == 1)
    {
        player.dirX -= PLAYER_SPEED;
        player.direction = LEFT;
        

        if(player.etat != WALK_LEFT && player.onGround == 1)
        {
            player.etat = WALK_LEFT;
          player.personnage[0] = IMG_Load("perso/habibLeft0.png");
          player.personnage[1] = IMG_Load("perso/habibLeft1.png");
          player.personnage[2] = IMG_Load("perso/habibLeft2.png");
          player.personnage[3] = IMG_Load("perso/habibLeft3.png");
          player.personnage[4] = IMG_Load("perso/habibLeft4.png");
          player.NumAnim = 0;
          playSoundFx(RUN);
          
        }
    }

    else if (input.right == 1)
    {
        player.dirX += PLAYER_SPEED;
        player.direction = RIGHT;
        
        

        if(player.etat != WALK_RIGHT && player.onGround == 1)
        {
            player.etat = WALK_RIGHT;
          player.personnage[0] = IMG_Load("perso/habib0.png");
          player.personnage[1] = IMG_Load("perso/habib1.png");
          player.personnage[2] = IMG_Load("perso/habib2.png");
          player.personnage[3] = IMG_Load("perso/habib3.png");
          player.personnage[4] = IMG_Load("perso/habib4.png");
          player.NumAnim = 0;
          playSoundFx(RUN);
        }
    }

    
    else if(input.right == 0 && input.left == 0 && player.onGround == 1)
    {
        

        if(player.etat != IDLE)
        {
            player.etat = IDLE;
            Mix_Pause(20);
            
            if(player.direction == LEFT)
            {
                player.personnage[0] = IMG_Load("perso/habibLeft0.png");
                player.personnage[1] = IMG_Load("perso/habibLeft0.png");
                player.personnage[2] = IMG_Load("perso/habibLeft0.png");
                player.personnage[3] = IMG_Load("perso/habibLeft0.png");
                player.personnage[4] = IMG_Load("perso/habibLeft0.png");
                player.NumAnim = 0;
                
            }
            else
            {
                player.personnage[0] = IMG_Load("perso/habib0.png");
                player.personnage[1] = IMG_Load("perso/habib0.png");
                player.personnage[2] = IMG_Load("perso/habib0.png");
                player.personnage[3] = IMG_Load("perso/habib0.png");
                player.personnage[4] = IMG_Load("perso/habib0.png");
                player.NumAnim = 0;
                
            }

        }

    }

  if (input.jump == 1)
    {
        if(player.onGround == 1)
        {
            player.dirY = -JUMP_HEIGHT;
            player.onGround = 0;
            player.jump = 1;
            playSoundFx(JUMP);
            Mix_Pause(20);
        }
        
        else if (player.jump == 1)
        {
            player.dirY = -JUMP_HEIGHT;
            player.jump = 0;
            playSoundFx(JUMP);
        }
        
        input.jump = 0;
    }

    
    if (player.onGround == 1)
        player.jump = 1;

    
    

  
    if(player.onGround == 0)
    {
        if(player.direction == RIGHT && player.etat != JUMP_RIGHT)
        {
            player.etat = JUMP_RIGHT;
            player.personnage[0] = IMG_Load("perso/saute1.png");
            player.personnage[1] = IMG_Load("perso/saute1.png");
            player.personnage[2] = IMG_Load("perso/saute1.png");
            player.personnage[3] = IMG_Load("perso/saute1.png");
            player.personnage[4] = IMG_Load("perso/saute1.png");
            player.NumAnim = 0;

        }
        else if(player.direction == LEFT && player.etat != JUMP_LEFT)
        {
            player.etat = JUMP_LEFT;
            player.personnage[0] = IMG_Load("perso/saute2.png");
            player.personnage[1] = IMG_Load("perso/saute2.png");
            player.personnage[2] = IMG_Load("perso/saute2.png");
            player.personnage[3] = IMG_Load("perso/saute2.png");
            player.personnage[4] = IMG_Load("perso/saute2.png");

            player.NumAnim = 0;
        }

         

    }



    
    mapCollision(&player);
    centerScrollingOnPlayer(maxW, maxH);

  }

    

    if (player.timerMort > 0)
    {
        player.timerMort--;

        if (player.timerMort == 0)
        {
            
            initializePlayer();
            jeu.etoiles=0;
            loadMap("images/map1.txt");
            jeu.vies--;
              if(jeu.vies < 0)
            {
              
                jeu.onMenu = 1;
                jeu.menuType = START;
            }
            
            
        }
    }
    //free(mort);

}



void centerScrollingOnPlayer(int maxW, int maxH)
 {
    map.startX = player.x - (maxW / 2);

    if (map.startX < 0)
    {
        map.startX = 0;
    }

    else if (map.startX + maxW >= map.maxX)
    {
        map.startX = map.maxX - maxW;
    }

    map.startY = player.y - (maxH / 2);

    if (map.startY < 0)
    {
        map.startY = 0;
    }

    else if (map.startY + maxH >= map.maxY)
    {
        map.startY = map.maxY - maxH;
    }
 }



     void drawplayer2()
{
    
    SDL_Rect dest;

    dest.x = player.x +player.dirX;
    dest.y = player.y ;
    dest.w = PLAYER_WIDTH;
    dest.h = PLAYER_HEIGTH;

   

    SDL_BlitSurface(player.personnage[player.NumAnim], NULL, jeu.ecran, &dest);
    SDL_Flip(jeu.ecran);


}




void updatePlayer2(int maxW, int maxH)
{ if(input.right == 0 && input.left == 0 && player.onGround == 1)
    {

        

        if(player.etat != IDLE)
        {
            player.etat = IDLE;
            
            if(player.direction == LEFT)
            {
              
           
                player.personnage[0] = IMG_Load("perso/habibLeft0.png");
                player.personnage[1] = IMG_Load("perso/habibLeft0.png");
                player.personnage[2] = IMG_Load("perso/habibLeft0.png");
                player.personnage[3] = IMG_Load("perso/habibLeft0.png");
                player.personnage[4] = IMG_Load("perso/habibLeft0.png");
                
                player.NumAnim = 0;
            }
            else
            {
              
          
                player.personnage[0] = IMG_Load("perso/habib0.png");
                player.personnage[1] = IMG_Load("perso/habib0.png");
                player.personnage[2] = IMG_Load("perso/habib0.png");
                player.personnage[3] = IMG_Load("perso/habib0.png");
                player.personnage[4] = IMG_Load("perso/habib0.png");
                 
                player.NumAnim = 0;
            }

        }

    }
    

  if(player.onGround == 0)
    {
        if(player.direction == RIGHT && player.etat != JUMP_RIGHT)
        {
           
         

            player.etat = JUMP_RIGHT;
            player.personnage[0] = IMG_Load("perso/saute1.png");
            player.personnage[1] = IMG_Load("perso/saute1.png");
            player.personnage[2] = IMG_Load("perso/saute1.png");
            player.personnage[3] = IMG_Load("perso/saute1.png");
            player.personnage[4] = IMG_Load("perso/saute1.png");
            
            player.NumAnim = 0;

        }
        else if(player.direction == LEFT && player.etat != JUMP_LEFT)
        {
          
      
            player.etat = JUMP_LEFT;
            player.personnage[0] = IMG_Load("perso/saute2.png");
            player.personnage[1] = IMG_Load("perso/saute2.png");
            player.personnage[2] = IMG_Load("perso/saute2.png");
            player.personnage[3] = IMG_Load("perso/saute2.png");
            player.personnage[4] = IMG_Load("perso/saute2.png");
        
            player.NumAnim = 0;
        }

         

    }





  if (input.left == 1)
    {
        player.dirX -= PLAYER_SPEED-8;
        player.direction = LEFT;

        if(player.etat != WALK_LEFT && player.onGround == 1)
        {
      
     
            player.etat = WALK_LEFT;
          player.personnage[0] = IMG_Load("perso/habibLeft0.png");
          player.personnage[1] = IMG_Load("perso/habibLeft1.png");
          player.personnage[2] = IMG_Load("perso/habibLeft2.png");
          player.personnage[3] = IMG_Load("perso/habibLeft3.png");
          player.personnage[4] = IMG_Load("perso/habibLeft4.png");
       
          player.NumAnim = 0;
          
        }
    }


    else if (input.right == 1)
    {
        player.dirX += PLAYER_SPEED -8;
        player.direction = RIGHT;
        

        if(player.etat != WALK_RIGHT && player.onGround == 1)
        {
          
        
            player.etat = WALK_RIGHT;
          player.personnage[0] = IMG_Load("perso/habib0.png");
          player.personnage[1] = IMG_Load("perso/habib1.png");
          player.personnage[2] = IMG_Load("perso/habib2.png");
          player.personnage[3] = IMG_Load("perso/habib3.png");
          player.personnage[4] = IMG_Load("perso/habib4.png");
        
          player.NumAnim = 0;
        }

    }


  if (player.timerMort==0 && player.y < maxH-250)//a modifier 3asba

  {

    player.dirY+=2.2;

  }

  if (player.dirY>=200)
  {
    player.dirY=200;
  }
  if (player.y>=maxH-250)//a modifier 3asba
  {
    player.onGround=1;
  }
  if (player.y > maxH-250 && player.onGround==1)
  {
    player.dirY=0;
    player.y=maxH-250;
  }

  
player.y=player.y + player.dirY;

if (input.left==1)
  {
    player.x-=PLAYER_SPEED;
  
  if (player.x<0)
  {
    player.x=0;
  }
  if (player.direction==RIGHT)
  {
    
 
    player.direction=LEFT;
    player.personnage[0] = IMG_Load("perso/habibLeft0.png");
    player.personnage[1] = IMG_Load("perso/habibLeft1.png");
    player.personnage[2] = IMG_Load("perso/habibLeft2.png");
    player.personnage[3] = IMG_Load("perso/habibLeft3.png");
    player.personnage[4] = IMG_Load("perso/habibLeft4.png");
    player.NumAnim = 0;
  }
}
  else if (input.right==1)
  {
    player.x+=PLAYER_SPEED;
    if (player.x+PLAYER_WIDTH >= maxW)
    {
      player.x=maxW-PLAYER_WIDTH;

    }
     if (player.direction==LEFT)
  {
    
   
    player.direction=RIGHT;

    player.personnage[0] = IMG_Load("perso/habib0.png");
    player.personnage[1] = IMG_Load("perso/habib1.png");
    player.personnage[2] = IMG_Load("perso/habib2.png");
    player.personnage[3] = IMG_Load("perso/habib3.png");
    player.personnage[4] = IMG_Load("perso/habib4.png");

    player.NumAnim = 0;

  }
}
if (input.jump==1 && player.onGround==1)
{
  player.onGround=0;
  player.dirY=player.dirY-30;

player.y=player.y + player.dirY;
  
  if (player.y>maxH-250)
  {
    player.onGround=1;
    player.dirY=0;
  }

  

  

}
if(jeu.vies < 0)
{
playSoundFx(LOOSE);
jeu.onMenu = 1;
jeu.menuType = START;



}




}